import { dropRightWhile } from 'es-toolkit';

// const drop_ = <T>(arr: T[], itemsCount: number): T[] => {
//   return arr.slice(itemsCount);
// };
// const dropRight_ = <T>(arr: T[], itemsCount: number): T[] => {
//   return arr.slice(0, arr.length - itemsCount);
// };

// // console.log(drop_([1, 2, 3, 4, 5], 2));
// console.log(dropRight_([1, 2, 3, 4, 5], 2));

// // Returns: [3, 4, 5]

// // 如果要移除的数量大于数组长度,则返回空数组
// console.log(drop_([1, 2, 3], 5));
// // Returns: []

const dropRightWhile_ = <T>(arr: T[], callBack: (item: T) => boolean) => {
  let start = arr.length - 1;
  while (callBack(arr[start--])) {
    arr.length = start + 1;
  }
  return arr;
};

const numbers = [1, 2, 3, 4, 5];
console.log(dropRightWhile_(numbers, (x) => x > 3));

const users = [
  { name: 'Alice', active: true },
  { name: 'Bob', active: true },
  { name: 'Charlie', active: false },
  { name: 'Bo2', active: true },
  { name: 'David', active: false },
];
console.log(dropRightWhile_(users, (user) => !user.active));
console.log(dropRightWhile(users, (user) => !user.active));
